package com.cn.web.controller.bc;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import com.cn.bean.pojo.bc.*;
import com.cn.bean.vo.bc.UserDepartmentVo;
import com.cn.config.config.Msg;
import com.cn.service.server.bc.AssistServer;
import com.cn.service.server.bc.MaterialServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

@Transactional
@CrossOrigin
@RestController
public class MaterialController {
    private final MaterialServer materialServer;
    private final AssistServer assistServer;
    @Autowired
    public MaterialController(MaterialServer materialServer,AssistServer assistServer) {
        this.materialServer = materialServer;
        this.assistServer = assistServer;

    }

    //修改
    @RequestMapping("/select/MaterialList")
    @ResponseBody
    public List<MaterialList> selectMaterialList(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        List<MaterialList> materialLists = materialServer.selectMaterialList(id);
        return materialLists;
    }

    //修改主表
    @RequestMapping("/update/Material")
    @ResponseBody
    public Msg updateMaterial(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        String userName = jsonObject.getString("userName");
        String cslx = jsonObject.getString("cslx");
        String cpyt = jsonObject.getString("cpyt");
        String kh = jsonObject.getString("kh");
        String cz = jsonObject.getString("cz");
        BigDecimal hd = jsonObject.getBigDecimal("hd");
        BigDecimal kd = jsonObject.getBigDecimal("kd");
        String csfs = jsonObject.getString("csfs");
        String dwjs = jsonObject.getString("dwjs");
        String bz = jsonObject.getString("bz");
        String fabh = jsonObject.getString("fabh");
        String famc = jsonObject.getString("famc");
        String xgsj = jsonObject.getString("xgsj");
        xgsj = xgsj.replace("/", "-");
//        List<Material> materials1 = materialServer.selectMaterialFamc(famc);
//        if (!materials1.isEmpty()) {
//            msg.setCode(500);
//            msg.setMessage("方案名称重复,请重新输入");
//            System.out.println("方案名称重复");
//            return msg;
//        }
        List<Assist> assists = assistServer.selectFabh(fabh);
        if (!assists.isEmpty()) {
            msg.setCode(500);
            msg.setMessage("该单据已被引用,不能修改");
            return msg;
        }
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        List<Department> departments = materialServer.selectDepartment(users.get(0).getUserId());
        String bmmc = departments.get(0).getBmmc();
        int i = materialServer.updateMaterial(cslx, cpyt, kh, cz, hd, kd, csfs, dwjs, bz, name, bmmc, fabh,famc,xgsj);
        List<Material> materials = materialServer.selectMat(fabh);
        int i1 = materialServer.deleteMaterialList(materials.get(0).getId());
        JSONArray requestDataArray = jsonObject.getJSONArray("requestData");
        // 遍历数组
        for (int j = 0; j < requestDataArray.size(); j++) {
            JSONObject item = requestDataArray.getJSONObject(j);
            int materialList = materialServer.getMaterialList() + 1;
            String wlbm = item.getString("wlbm");
            String wlmc = item.getString("wlmc");
            String wlgg = item.getString("wlgg");
            String jldw = item.getString("jldw");
            BigDecimal bzsl = item.getBigDecimal("bzsl");
            BigDecimal bzdj = item.getBigDecimal("bzdj");
            BigDecimal bzje = item.getBigDecimal("bzje");
            String bzList = item.getString("bz1");
            if (wlbm.isEmpty() || wlmc.isEmpty()) {
                break;
            }
            int i3 = materialServer.addMaterialList(materialList, wlbm, wlmc, wlgg, jldw, bzsl, bzdj, bzje, bzList, csfs, dwjs, materials.get(0).getId());
        }
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("修改成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("修改失败");
        return msg;
    }

    //进入页面
    @RequestMapping("/select/Material")
    @ResponseBody
    public List<Material> selectMaterial(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        int page = jsonObject.getIntValue("page");
        int limit = jsonObject.getIntValue("limit");
        String cpyt = jsonObject.getString("cpyt");
        String fabh = jsonObject.getString("fabh");
        cpyt = (cpyt != null) ? cpyt : "";
        fabh = (fabh != null) ? fabh : "";

        List<Material> materials = materialServer.selectMaterial(limit, page, cpyt, fabh);
        return materials;
    }
   //查看已经审核的
    @RequestMapping("/select/Material1")
    @ResponseBody
    public List<Material> selectMaterial1(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        int page = jsonObject.getIntValue("page");
        int limit = jsonObject.getIntValue("limit");
        String cpyt = jsonObject.getString("cpyt");
        String fabh = jsonObject.getString("fabh");
        cpyt = (cpyt != null) ? cpyt : "";
        fabh = (fabh != null) ? fabh : "";
        System.out.println("产品用途："+cpyt);
        System.out.println("方案编号："+fabh);
        List<Material> materials = materialServer.selectMaterial1(limit, page, cpyt, fabh);
        return materials;
    }
    @RequestMapping("/select/ywy")
    @ResponseBody
    public UserDepartmentVo select(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        List<Department> departments = materialServer.selectDepartment(users.get(0).getUserId());
        UserDepartmentVo userDepartmentVo = new UserDepartmentVo();
        userDepartmentVo.setUserId(users.get(0).getUserId());
        userDepartmentVo.setName(users.get(0).getName());
        userDepartmentVo.setBmmc(departments.get(0).getBmmc());
        //部门工号
        userDepartmentVo.setDepartmentId(departments.get(0).getId());
        return userDepartmentVo;
    }

    @RequestMapping("/get/ywy")
    @ResponseBody
    public List<UserDepartmentVo> getYwy(@RequestBody JSONObject jsonObject) {
        Integer limit = jsonObject.getInteger("limit");
        Integer page = jsonObject.getInteger("page");
        List<UserDepartmentVo> ywy = materialServer.getYwy(limit, page);

        return ywy;
    }
    @RequestMapping("/get/Search/ywy")
    @ResponseBody
    public List<User> getSearchYwy(@RequestBody JSONObject jsonObject) {
        Integer limit = jsonObject.getInteger("limit");
        Integer page = jsonObject.getInteger("page");
        String inputYwybh = jsonObject.getString("inputYwybh");
        String inputYwymc = jsonObject.getString("inputYwymc");
        inputYwybh = (inputYwybh != null) ? inputYwybh : "";
        inputYwymc = (inputYwymc != null) ? inputYwymc : "";
        List<User> user = materialServer.getUser(limit, page, inputYwybh, inputYwymc);
        return user;
    }
    @RequestMapping("/add/Material")
    @ResponseBody
    public Msg addMaterial(@RequestBody JSONObject jsonObject) throws ParseException {
        Msg msg = new Msg();
        String cpyt = jsonObject.getString("cpyt");
        String kh = jsonObject.getString("kh");
        String cz = jsonObject.getString("cz");
        BigDecimal hd = jsonObject.getBigDecimal("hd");
        BigDecimal kd = jsonObject.getBigDecimal("kd");
        String cslx = jsonObject.getString("cslx");
        String csfs = jsonObject.getString("csfs");
        String dwjs = jsonObject.getString("dwjs");
        String ywy = jsonObject.getString("ywy");
        //用户名==工号
        String ywygh = jsonObject.getString("userName");
        String ywbm = jsonObject.getString("ywbm");
        String ywbmgh = jsonObject.getString("ywbmgh");
        String zdr = jsonObject.getString("zdr");
        String bz = jsonObject.getString("bz");
        String zdbm = jsonObject.getString("zdbm");
        String zdsjStr = jsonObject.getString("zdsj");
        String djrqStr = jsonObject.getString("djrq");
        String famc = jsonObject.getString("famc");
        String sszz = jsonObject.getString("sszz");
        zdsjStr = zdsjStr.replace("/", "-");
        djrqStr = djrqStr.replace("/", "-");
        //判断方案名称是否重复
        List<Material> materials = materialServer.selectMaterialFamc(famc);
        if (!materials.isEmpty()) {
            msg.setCode(500);
            msg.setMessage("方案名称重复,请重新输入");
            return msg;
        }
        int id = materialServer.getMaterial() + 1;
        String fabh = String.valueOf(materialServer.getMaterial() + 80000000);
        int i = materialServer.addMaterial(id, fabh, djrqStr, 0, cslx, cpyt, kh, cz, hd, kd, csfs, dwjs, ywy, ywygh, ywbm, ywbmgh, bz, zdr, zdbm, zdsjStr, "", "", "", "",famc,sszz);
        JSONArray requestDataArray = jsonObject.getJSONArray("requestData");
        // 遍历数组
        int i2 = 1;
        for (int j = 0; j < requestDataArray.size(); j++) {
            JSONObject item = requestDataArray.getJSONObject(j);
            int materialList = materialServer.getMaterialList() + 1;
            String wlbm = item.getString("wlbm");
            String wlmc = item.getString("wlmc");
            String wlgg = item.getString("wlgg");
            String jldw = item.getString("jldw");
            BigDecimal bzsl = item.getBigDecimal("bzsl");
            BigDecimal bzdj = item.getBigDecimal("bzdj");
            BigDecimal bzje = item.getBigDecimal("bzje");
            String bzList = item.getString("bz1");
            if (wlbm.isEmpty() || wlmc.isEmpty()) {
                break;
            }
            int i1 = materialServer.addMaterialList(materialList, wlbm, wlmc, wlgg, jldw, bzsl, bzdj, bzje, bzList, csfs, dwjs, id);
            if (i1 == 0) {
                i2 = 0;
            }
        }
        if (i > 0 && i2 > 0) {
            msg.setCode(200);
            msg.setMessage("添加成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("添加失败");
        return msg;
    }

    @RequestMapping("/delete/Material")
    @ResponseBody
    public Msg deleteMaterial(@RequestBody JSONObject jsonObject) {
        int id = jsonObject.getIntValue("id");
        String userName = jsonObject.getString("userName");
        String fabh = jsonObject.getString("fabh");
        Msg msg = new Msg();
        List<Assist> assists = assistServer.selectFabh(fabh);
        if (!assists.isEmpty()) {
            msg.setCode(500);
            msg.setMessage("该单据已被引用,不能删除");
            return msg;
        }
        Material material = materialServer.selectDjzt(id);
        int djzt = material.getDjzt();
        if (djzt > 0) {
            msg.setCode(500);
            msg.setMessage("该单据已审核，不能删除");
            return msg;
        }
        int i = materialServer.deleteMaterial(id);
        int i1 = materialServer.deleteMaterialList(id);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("删除成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("删除失败");
        return msg;
    }

    @RequestMapping("/up/auto0")
    @ResponseBody
    public Msg upauto0(@RequestBody JSONObject jsonObject) {
        int id = jsonObject.getIntValue("id");
        String userName = jsonObject.getString("userName");
        String shsj = jsonObject.getString("shsj");
        shsj = shsj.replace("/", "-");
        Msg msg = new Msg();
        Material material = materialServer.selectDjzt(id);
        List<User> users = materialServer.selectUser(userName);

        int djzt = material.getDjzt();
        if (djzt>0){
            msg.setCode(500);
            msg.setMessage("单据请勿重复审核");
            return msg;
        }
        int i = materialServer.intupDateAuto0(id, userName, users.get(0).getBmmc(),shsj);

        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("审核成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("审核失败");
        return msg;
    }

    @RequestMapping("/up/auto1")
    @ResponseBody
    public Msg upauto1(@RequestBody JSONObject jsonObject) {
        int id = jsonObject.getIntValue("id");
        String userName = jsonObject.getString("userName");
        String shsj = jsonObject.getString("shsj");
        String fabh = jsonObject.getString("fabh");
        shsj = shsj.replace("/", "-");
        Msg msg = new Msg();
        List<Assist> assists = assistServer.selectFabh(fabh);
        List<User> users = materialServer.selectUser(userName);

        if (!assists.isEmpty()) {
            msg.setCode(500);
            msg.setMessage("该单据已被引用,不能弃审");
            return msg;
        }
        Material material = materialServer.selectDjzt(id);
        int djzt = material.getDjzt();
        if (djzt==0){
            msg.setCode(500);
            msg.setMessage("单据请勿重复弃审");
            return msg;
        }
        int i = materialServer.intupDateAuto1(id, userName,users.get(0).getBmmc(), shsj);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("弃审成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("弃审失败");
        return msg;
    }

    //查看用途
    @RequestMapping("/Material/Select/UseDefinition")
    @ResponseBody
    public List<UseDefinition> MaterialSelectUseDefinition(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String ytbm = jsonObject.getString("ytbm");
        String ytfl = jsonObject.getString("ytfl");
        String ytmc = jsonObject.getString("ytmc");
        ytbm = (ytbm != null) ? ytbm : "";
        ytfl = (ytfl != null) ? ytfl : "";
        ytmc = (ytmc != null) ? ytmc : "";
        List<UseDefinition> useDefinitions = materialServer.selectUseDefinition(limit, page, ytbm, ytfl, ytmc);
        return useDefinitions;
    }
    @RequestMapping("/Material/Select/UseDefinition/Mc")
    @ResponseBody
    public List<UseDefinition> MaterialSelectUseDefinitionMc(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String ytbm = jsonObject.getString("input1");
        String ytfl = jsonObject.getString("input2");
        String ytmc = jsonObject.getString("input3");
        ytbm = (ytbm != null) ? ytbm : "";
        ytfl = (ytfl != null) ? ytfl : "";
        ytmc = (ytmc != null) ? ytmc : "";
        List<UseDefinition> useDefinitions = materialServer.selectUseDefinition(limit, page, ytbm, ytfl, ytmc);
        return useDefinitions;
    }
//查看材质
    @RequestMapping("/Mat/Select/UseDefinition")
    @ResponseBody
    public List<MaterList> MatSelectUseDefinition(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String ytbm = jsonObject.getString("ytbm");
        String ytfl = jsonObject.getString("ytfl");
        String ytmc = jsonObject.getString("ytmc");
        ytbm = (ytbm != null) ? ytbm : "";
        ytfl = (ytfl != null) ? ytfl : "";
        ytmc = (ytmc != null) ? ytmc : "";
        List<MaterList> useDefinitions = materialServer.selectMater(limit, page, ytbm, ytfl, ytmc);
        return useDefinitions;
    }
//    查看材质2



    @RequestMapping("/Mat/Select/UseDefinition/Mc")
    @ResponseBody
    public List<MaterList> MatSelectUseDefinitionMc(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String ytbm = jsonObject.getString("input1");
        String ytfl = jsonObject.getString("input2");
        String ytmc = jsonObject.getString("input3");
        ytbm = (ytbm != null) ? ytbm : "";
        ytfl = (ytfl != null) ? ytfl : "";
        ytmc = (ytmc != null) ? ytmc : "";
        List<MaterList> useDefinitions = materialServer.selectMater(limit, page, ytbm, ytfl, ytmc);
        return useDefinitions;
    }
    @RequestMapping("/Mat/Select/UseDefinition/Mc/A")
    @ResponseBody
    public List<MaterList> MatSelectUseDefinitionMcA(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String ytbm = jsonObject.getString("input1");
        String ytfl = jsonObject.getString("input2");
        String ytmc = jsonObject.getString("input3");
        ytbm = (ytbm != null) ? ytbm : "";
        ytfl = (ytfl != null) ? ytfl : "";
        ytmc = (ytmc != null) ? ytmc : "";
        List<MaterList> useDefinitions = materialServer.selectMaterA(limit, page, ytbm, ytfl, ytmc);
        return useDefinitions;
    }
}
